package com.none.web.service.voting.impl;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.none.core.common.utils.CreateID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.none.core.common.utils.StringUtil;
import com.none.core.exception.ValidateException;
import com.none.core.model.Pager;
import com.none.core.service.BaseServiceSupport;
import com.none.web.model.TActivity;
import com.none.web.model.TActivityAppUser;
import com.none.web.model.TActivityImage;
import com.none.web.model.TTags;
import com.none.web.model.TUser;
import com.none.web.model.TUserApp;
import com.none.web.model.TVotingAppUser;
import com.none.web.service.activity.IActivityService;
import com.none.web.service.voting.IVotingService;

@Service("votingService")
public class VotingServiceImpl extends BaseServiceSupport implements
		IVotingService {

	@Autowired
	JdbcTemplate jdbcTemplate;
	/*
	 * join voting add by William 20150212
	 * @see com.none.web.service.voting.IVotingService#joinVoting(java.lang.String, java.lang.String, java.lang.String[], java.lang.String[])
	 */
	public boolean joinVoting(String votingId, String userId,
			String[] optionsId, String[] itemsId) throws Exception {
		if (this.checkVoting(votingId, userId)) {
			if (optionsId.length != itemsId.length) {
				throw new ValidateException("voting.paramsError");
			}
			TUserApp addUser = findEntity(TUserApp.class, userId);
			for (int i = 0; i < optionsId.length; i++) {
				TVotingAppUser vAppuser = new TVotingAppUser();
				String id = CreateID.getID();
				vAppuser.setId(id);
				vAppuser.setUserId(addUser.getUserId());
				vAppuser.setVotingId(votingId);
				vAppuser.setItemId(itemsId[i]);
				vAppuser.setOptionId(optionsId[i]);
				vAppuser.setVotedTime(new Timestamp(new Date().getTime()));
				if (saveEntity(vAppuser) == null) {
					throw new ValidateException("voting.appIsTwo");
				}
			}
		}
		return true;
	}

	/*
	 * check voting add by William 20150212
	 * @see com.none.web.service.voting.IVotingService#checkVoting(java.lang.String, java.lang.String)
	 */
	public boolean checkVoting(String votingId, String userId) throws Exception {
		// 判断用户登录是否合法
		if (StringUtils.isBlank(userId)) {
			throw new ValidateException("user.userUnExist");
		}
		TUserApp addUser = findEntity(TUserApp.class, userId);
		if (addUser == null) {
			throw new ValidateException("user.userUnExist");
		}
		// 防止重复报名
		Criteria criteria = getCriteria(TVotingAppUser.class);
		criteria.add(Restrictions.eq("userId", addUser.getUserId()));
		criteria.add(Restrictions.eq("votingId", votingId));
		Object userRows = criteria.setProjection(Projections.rowCount())
				.uniqueResult();
		if (userRows != null) {
			long l = Long.valueOf(userRows.toString());
			if (l > 0) {
				throw new ValidateException("voting.appIsTwo");
			}
		}
		return true;
	}

}
